/*
 * FilterWordBase.h
 *
 *  Created on: 2011-4-21
 *      Author: lgw
 */

#pragma once

#include <map>
#include <string>
#include <vector>

namespace Zha
{

class FilterWordBase
{
public:
	class Filter
	{
		class Node
		{
		public:
			typedef std::map<wchar_t, Node*> children_t;

		public:
			Node() :m_Leaf(false), m_Children() { }
			virtual ~Node() { }

			children_t& GetChildren()
			{
				return m_Children;
			}

			void SetKey(wchar_t key) { m_Key = key; }
			void SetLeaf(bool leaf) { m_Leaf = leaf; }
			bool IsLeaf() { return m_Leaf; }

		private:
			wchar_t m_Key;
			bool m_Leaf;
			children_t m_Children;
		};

	public:
		Filter(std::vector<std::string> &fwList);
		virtual ~Filter()
		{
			if (m_Root)
			{
				delete m_Root;
				m_Root = NULL;
			}
		}

		bool Find(const std::string &text);

	private:
		bool DoFind(Node* node, size_t pos, const std::wstring &text);
		std::wstring ToTrimUpper(std::wstring word);
		std::wstring NarrowToWide(const std::string &text);

	private:
		Node* m_Root;
	};

public:
	FilterWordBase() { m_Filter = NULL; }
	virtual ~FilterWordBase()
	{
		if (m_Filter)
		{
			delete m_Filter;
			m_Filter = NULL;
		}
	}

public:
	bool Init();
	bool Load();
	int Find(const std::string &text);
	void AddFilterWord(std::string str);
	Filter* GetFilter()
	{
		return m_Filter;
	}

private:
	std::vector<std::string> m_filterWords;
	Filter* m_Filter;

};

}
